home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 16 / sets / setsourc / setof.c < prev    next >
C/C++ Source or Header  |  1989-03-09  |  5KB  |  135 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "sets.h"
  4. /***************************************************************************/
  5.                   int set_of(set *aset,...)
  6. /***************************************************************************/
  7. /*    This function assigns a set of values to a set variable after clearing
  8.    any contents of that variable.  The variable's parameters must be able
  9.    to accomodate the set of values.
  10.  
  11.    Base_Type  Set_Size  Nmembers    Member                Notes:
  12.                                    Expression
  13.     --------------------------------------------------------------------------
  14.     CHARACTER    256            0           -        Nmembers=0 means empty set.
  15.     CHARACTER      256          1-256       Subranges    See SUBRANGE, below.
  16.     CHARACTER    256          1-256       member[i],member[j],...
  17.                                                 List members. Combine with
  18.                                                 Subrange(s).
  19.     BOOLEAN          2            0            -        Nmembers=0 means empty set.
  20.     BOOLEAN          2           1-2       TRUE/FALSE    One or two members containing
  21.                                                 the values for TRUE/FALSE.
  22.     ENUMERATED       m            0           -        Nmembers=0 means empty set.
  23.     ENUMERATED       m            n       member[i],member[j],...
  24.                                                 List members.
  25.     SUBRANGE      m            0            -        Nmembers=0 means empty set.
  26.     SUBRANGE      m            n       member[i]__member[j]
  27.                                                 Pascal subrange notation (almost).
  28.                                                 For CHARACTER, ENUMERATED, and
  29.                                                 decimal base_types. i..j is
  30.                                                 specified contiguously.
  31.     SUBRANGE      m            n       member[i],...,member[j]
  32.                                                 List members.
  33. -----------------------------------------------------------------------------
  34.  
  35.     General parameter list configuration:
  36.  
  37.     set_of(set-pointer,...members_list...,_E)
  38.  
  39.     where:- set-pointer is a pointer to a set structure defined by the
  40.             macro, defset.
  41.           - tag-value is a unique value for use by the programmer to force
  42.             set type checking with each tag value representing a unique
  43.             set-type.
  44.           - members_list is any combination of members list and subrange.
  45.           - nmembers is the number of members specified in the members_list.
  46.           - nmembers = 0 means empty set and requires no members_list.
  47.           - set_size is the largest possible number of members in the set.
  48.           - Base_type is one of the four members of the enumerated type, base,
  49.             and is the type against which checking is done in set operations.
  50.           - a set variable is an empty set.
  51.           - the term, "_E", is the end-of-set delimiter and must be used to
  52.             terminate a members_list.
  53.  
  54. ----------------------------------------------------------------------------
  55. */
  56. {
  57. va_list ap;
  58.  
  59.     /* clear the destination set */
  60.     set_clear(aset);
  61.  
  62.     /* Enter the members into the member records.  Parse
  63.        the remainder of the parameter list, determine the actual parameter
  64.        locations in the member records and set the appropriate bits to record
  65.        the presence of the member in the set.  NOTE:  All member types are
  66.        assumed to be integer.  For compilers that treat char as integer, this
  67.        will be no change.  Some compilers will keep a char parameter at char
  68.        length and some will "promote" a char variable to an integer parameter.
  69.        ANSI C (X3J11) as defined in "C Wizard's Programming Reference" by
  70.        W. David Schwaderer, Wiley Press, 1985, automatically promotes char or
  71.        short variables to integer in actual parameter lists.  (Note that byte
  72.        type is not mentioned).  We treat all members the same since all the
  73.        other base_types can be treated as type integer.
  74.  
  75.        The parameter list is of unknown length.  We don't know how the user
  76.        arranged the set members or if he used subrange notation which greatly
  77.        compresses the number of actual parameters despite a possibly large
  78.        number of members.
  79.  
  80.        For subrange notation, the notation, "__", is converted to ",-1," so
  81.        as to appear to be another integer parameter and 'a'__'z' becomes
  82.        'a',-1,'z'.  This change is made by the #define statement in the header
  83.        file, 'sets.h'.  Since members can only be positive integers, the -1
  84.        acts as a flag denoting a subrange bounded by the preceding and
  85.        following members.
  86.  
  87.        To end the list of set members, the end of list symbol, _E, is used.
  88.        This is converted by a #define statement in the header file to -2.
  89.        NOTE: To enter an empty set, include the _E symbol without any preceding
  90.        set members.
  91.     */
  92.     aset->nmembers = 0;
  93.     va_start(ap,aset);
  94.     if(set_args(aset,ap) == FAILURE)
  95.         return FAILURE;
  96.     va_end(ap);
  97.  
  98.     /* We're done.  Return SUCCESS. */
  99.     return SUCCESS;
  100.  
  101. }  /* end set_of */
  102.  
  103.  
  104. /***************************************************************************/
  105.                  int set_args(set *aset, va_list ap)
  106. /***************************************************************************/
  107. /* Extracts all the set members from a parameter list passed to the
  108.    caller of this function.
  109. */
  110. {
  111. int j,parm,lastparm;
  112.  
  113.     j = 0;
  114.     do    {
  115.         /* get a parameter -- NOTE: C-Enum types start at the zeroth member */
  116.         if((parm = va_arg(ap,int)) >= 0)
  117.             if(add_member(aset,parm) == FAILURE)    /* individual member */
  118.                 return FAILURE;
  119.             else
  120.                 lastparm = parm;
  121.                       
  122.         else if(parm == -1)
  123.             {                               /* "__" notation, a subrange */
  124.             parm = va_arg(ap,int);
  125.             for(j=lastparm+1;j<=parm;j++)
  126.                 if(add_member(aset,j) == FAILURE)
  127.                     return FAILURE;
  128.             }
  129.         }
  130.     while(parm != -2);
  131.     return SUCCESS;
  132.  
  133. }  /* end set_args */
  134.  
  135.